package com.bookstore.controller;

import com.alibaba.fastjson.JSON;
import com.bookstore.entity.Book;
import com.bookstore.entity.BookCar;
import com.bookstore.entity.Order;
import com.bookstore.entity.PageBean;
import com.bookstore.service.impl.OrderServiceImpl;
import org.aspectj.weaver.ast.Or;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @Author Cofe
 * @Date 2022/5/2 19:58
 */
@Controller
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderServiceImpl orderService;
    @RequestMapping(
            value = "/findallorder",
            method = RequestMethod.POST
    )
    @CrossOrigin
    @ResponseBody
    public List<Order> findbookcar(HttpSession httpSession) {
        Object user_uid1 = httpSession.getAttribute("user_uid");
        int user_uid=Integer.valueOf(user_uid1+"");
        List<Order> list=orderService.findorderbyid(user_uid);
        return list;
    }

    @RequestMapping(
            value = "/selectbypc",
            method = RequestMethod.POST,
            produces = {"application/text;charset=UTF-8"}
    )
    @CrossOrigin
    @ResponseBody
    public String selectByPageAndCondition(int currentPage, int pageSize, HttpSession httpSession){
        Object user_uid1 = httpSession.getAttribute("user_uid");
        int user_uid=Integer.valueOf(user_uid1+"");
        String username=httpSession.getAttribute("username")+"";
        //调用service查询
        PageBean<Order> orderPageBean = orderService.findourderbyPageAndCondition(currentPage, pageSize, username);
        //2. 转为JSON
        String jsonString = JSON.toJSONString(orderPageBean);
        return jsonString;
    }

    @RequestMapping(
            value = "/deleteOrder",
            method = RequestMethod.POST,
            produces = {"application/text;charset=UTF-8"}
    )
    @CrossOrigin
    @ResponseBody
    public String deleteOrderBybuyid(int buy_id){
        String s = orderService.deleteOrder(buy_id);
        return s;
    }

    //查询订单信息
    @RequestMapping(
            value = "/ordertoum",
            method = RequestMethod.POST
    )
    @CrossOrigin
    @ResponseBody
    public List<Order> orderToum(HttpSession httpSession) {
        Object user_uid1 = httpSession.getAttribute("username");
        String username=user_uid1+"";
        List<Order> list=orderService.selectOrderByUser(username);
        return list;
    }

    //签收订单
    @RequestMapping(
            value = "/receiptorder",
            method = RequestMethod.POST
    )
    @CrossOrigin
    @ResponseBody
    public String receiptOrder(int buy_id) {
        String s = orderService.receiptOrder(buy_id);
        return s;
    }

    //查询所有人订单
    //查询所有的用户订单
    @RequestMapping(
            value = "/selectbypageOrder",
            method = RequestMethod.POST,
            produces = {"application/text;charset=UTF-8"}
    )
    @CrossOrigin
    @ResponseBody
    public String selectByPageOrder(int currentPage,int pageSize) {
        PageBean<Order> orderPageBean = orderService.selectAllOrder(currentPage, pageSize);
        //2. 转为JSON777777
        String jsonString = JSON.toJSONString(orderPageBean);
        return jsonString;
    }
}
